﻿ using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using WordServices.HttpService;

// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

namespace WordsOfLove.ForumPages
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class ForumMainPage : Page
	{
		ObservableCollection<ForumPostInfo> HotPosts { get; set; }
		ObservableCollection<ForumPostInfo> RecentPosts { get; set; }

		public ForumMainPage()
        {
            this.InitializeComponent();
			HttpUtils utils = HttpUtilsFactory.Utils;
			try
			{
				List<ForumPostInfo> HotPostList = utils.SendGetJsonMessage<List<ForumPostInfo>>("api/ForumPost/GetHotPosts");
				HotPostList.Sort((x, y) => Comparer<int>.Default.Compare(y.LikeCount, x.LikeCount));
				HotPosts = new ObservableCollection<ForumPostInfo>(HotPostList);
				RecentPosts = new ObservableCollection<ForumPostInfo>(utils.SendGetJsonMessage<List<ForumPostInfo>>("api/ForumPost/GetRecentPosts"));
			}
			catch(Exception ex)
			{
				new MessageDialog("服务器错误").ShowAsync();
			}
			RefreshUser();
			HotPostListView.ItemsSource = HotPosts;
			RecentPostListView.ItemsSource = RecentPosts;
        }

		private void RefreshUser()
		{
			if (CurrentUserFactory.CurrentUser == null)
			{
				LoginBtn.Visibility = Visibility.Visible;
				LogoutBtn.Visibility = Visibility.Collapsed;
				CurrentUserNameTxt.Text = "未登录";
				CurrentUserFactory.CurrentUserId = -1;
				CreatePostBtn.IsEnabled = false;
				MyPostBtn.IsEnabled = false;
			}
			else
			{
				LoginBtn.Visibility = Visibility.Collapsed;
				LogoutBtn.Visibility = Visibility.Visible;
				CurrentUserNameTxt.Text = CurrentUserFactory.CurrentUser.UserName;
				CurrentUserFactory.CurrentUserId = CurrentUserFactory.CurrentUser.Id;
				CreatePostBtn.IsEnabled = true;
				MyPostBtn.IsEnabled = true;
			}
		}

		private void PostListView_ItemClick(object sender, ItemClickEventArgs e)
		{
			ForumPostInfo info = e.ClickedItem as ForumPostInfo;
			this.Frame.Navigate(typeof(PostDetailsPage), info);
		}

		private async void LoginBtn_Click(object sender, RoutedEventArgs e)
		{
			LoginDialog dialog = new LoginDialog();
			await dialog.ShowAsync();
			RefreshUser();
		}

		private void LogoutBtn_Click(object sender, RoutedEventArgs e)
		{
			CurrentUserFactory.CurrentUser = null;
			RefreshUser();
			LogoutBtn.Flyout.Hide();
		}

		private void CreatePostBtn_Click(object sender, RoutedEventArgs e)
		{
			this.Frame.Navigate(typeof(PostEditPage));
		}

		private void SearchBtn_Click(object sender, RoutedEventArgs e)
		{
			SearchPageNavigationArg arg = new SearchPageNavigationArg() { SearchContent = SearchBox.Text };
			switch (SearchOptionComboBox.SelectedIndex)
			{
				default:
				case 0: arg.Mode = SearchPageMode.ByTitle; break;
				case 1: arg.Mode = SearchPageMode.ByTag; break;
				case 2: arg.Mode = SearchPageMode.ByUser; break;
			}
			this.Frame.Navigate(typeof(SearchPage), arg);
		}

		private void MyPostBtn_Click(object sender, RoutedEventArgs e)
		{
			SearchPageNavigationArg arg = new SearchPageNavigationArg()
			{
				SearchContent = CurrentUserFactory.CurrentUserId.ToString(),
				Mode = SearchPageMode.ByUserId
			};
			this.Frame.Navigate(typeof(SearchPage), arg);
		}
	}

	public class CurrentUserFactory
	{
		public static User CurrentUser { get; set; }
		public static int CurrentUserId { get; set; }
	}
}
